package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.bo.promotionbo.AdminBo;
import com.cskaoyan.bean.vo.promotionvo.AdminAdVo;
import com.cskaoyan.bean.po.promotionpo.MarketAd;
import com.cskaoyan.mapper.promotion.AdminAdMapper;
import com.cskaoyan.mapper.promotion.MarketAdMapper;

import com.cskaoyan.util.common.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @desctription
 * @date since 2022-07-16 14:20
 */
@Service
public class AdminAdServiceImpl implements AdminAdService {
    @Autowired
    AdminAdMapper adminAdMapper;
    @Autowired
    MarketAdMapper marketAdMapper;

    @Override
    public CommonData getAds(AdminBo adminAdBo) {
        PageHelper.startPage(adminAdBo.getPage(), adminAdBo.getLimit());
        if (adminAdBo.getContent()!=null) {
            adminAdBo.setContent("%" + adminAdBo.getContent() + "%");
        }
        if (adminAdBo.getName()!=null) {
            adminAdBo.setName("%" + adminAdBo.getName() + "%");
        }
        List<AdminAdVo> adVoList = adminAdMapper.selectAdList(adminAdBo);
        PageInfo<AdminAdVo> adminAdVoPageInfo = new PageInfo<>(adVoList);
        return CommonData.data(adminAdVoPageInfo);
    }
    @Transactional
    @Override
    public MarketAd insertAds(MarketAd marketAd) {
        marketAd.setAddTime(DateUtils.time());
        marketAd.setUpdateTime(DateUtils.time());
        marketAd.setDeleted(false);
        marketAdMapper.insertSelective(marketAd);
        Integer id = marketAd.getId();
        MarketAd marketAd1 = marketAdMapper.selectByPrimaryKey(id);

        return marketAd1;
    }
    @Transactional
    @Override
    public MarketAd updateAds(MarketAd marketAd) {
        marketAd.setUpdateTime(DateUtils.time());
        marketAdMapper.updateByPrimaryKeySelective(marketAd);

        return marketAd;
    }

    @Transactional
    @Override
    public void deleteAds(MarketAd marketAd) {
        marketAd.setDeleted(true);
        marketAdMapper.updateByPrimaryKey(marketAd);

    }

    @Override
    public MarketAd readAdminAd(Integer id) {
        MarketAd marketAd = marketAdMapper.selectByPrimaryKey(id);
        return marketAd;

    }


}
